import { _decorator, Component, director, Label, Node, Sprite } from 'cc';
import { gameConfig } from '../data/gameConfig';
import { nodePool } from '../utils/nodePool';
import { audioTool } from '../utils/audioTool';
import { load } from '../utils/tools';
import { loadRes } from '../utils/loadRes';
import { emits } from '../data/enums';
import { LevelManager } from '../utils/LevelManager';
import { init } from '../scene/init';
const { ccclass, property } = _decorator;

@ccclass('levelView')
export class levelView extends Component {
    @property(Sprite)
    musicBtn: Sprite = null;
    @property(Label)
    starNum: Label = null;
    private levelLay = null;
    protected onEnable(): void {
        this.levelLay = this.node.getChildByName("levelList");
        this.initUI();
        this.initLevelList(gameConfig.currChapter);
    }
    initUI() {
        //初始化星星数显示
        let starAll = 0;
        for (let i = 0; i < gameConfig.levelList.length; i++) {
            starAll = gameConfig.levelList.length * 3 - 3;
        }
        let starGet = 0;
        for (let i = 0; i < gameConfig.levelList.length; i++) {
            if (gameConfig.levelList[i] > -1) {
                console.log(gameConfig.levelList[i]);
                starGet += gameConfig.levelList[i];
            }
        }
        this.starNum.string = `${starGet}/${starAll}`;
        //初始化音乐图标
        if (gameConfig.isBgm) {
            this.musicBtn.spriteFrame = loadRes.ins.getSpriteFrame("musicOn");
        } else {
            this.musicBtn.spriteFrame = loadRes.ins.getSpriteFrame("musicOff");
        }
    }

    //初始化关卡列表
    initLevelList(chapter: number) {
        this.levelLay.removeAllChildren();
        //关卡图标
        console.log(gameConfig.maxLevel);
        for (let i = 0; i < gameConfig.levelNums; i++) {
            if (i + (gameConfig.currChapter-1) * gameConfig.levelNums < gameConfig.maxLevel-1) {
                let item = nodePool.ins.getPoolNode("levelItem", this.levelLay);
                let itemTs: any = item.getComponent("levelItem");
                itemTs.levelNum = i + 1;
                itemTs.init();
            }
        }
    }
    clickHome() {
        director.loadScene("main");
    }
    clickMusic() {
        gameConfig.isBgm = !gameConfig.isBgm;
        if (gameConfig.isBgm) {
            audioTool.ins.playMusic("bgm");
            this.musicBtn.spriteFrame = loadRes.ins.getSpriteFrame("musicOn");
        } else {
            audioTool.ins.stopMusic();
            this.musicBtn.spriteFrame = loadRes.ins.getSpriteFrame("musicOff");
        }

    }
    clickRight() {
        if (gameConfig.currChapter >= 2) {
            nodePool.ins.getPoolNode("toast", this.node);
            director.emit(emits.toast, "暂未开放");
            return;
        }
        gameConfig.currChapter++;
        this.initLevelList(gameConfig.currChapter);
    }
    clickLeft() {
        if (gameConfig.currChapter > 1) {
            gameConfig.currChapter--;
            this.initLevelList(gameConfig.currChapter);
        }
    }
}


